package phone.qyg.com.qyg.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.application.QXApplication;
import com.app.base.tool.BaseActivity;
import com.app.config.AppHttpKey;
import com.app.config.ApplicationConfig;
import com.app.utils.PromptManager;
import com.app.utils.TransformController;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.CourseInterTrans;
import phone.qyg.com.qyg.utils.AppHttpRequest;

/**
 * 作者：lblbh on 2017/12/8 17:36
 * 邮箱：lanbuhan@163.com
 * 功能：发送回复
 */
@ContentView(R.layout.releas_interlocu)
public class CourseReleaseInterlocuActivity extends BaseActivity implements TextWatcher {

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.submit)
    TextView submit;

    @ViewInject(R.id.content)
    EditText content;

    @ViewInject(R.id.title)
    EditText title;

    @ViewInject(R.id.title_linear)
    RelativeLayout title_linear;

    CourseInterTrans trans;

    int contentHint = R.string.please_problem;

    int object_id;

    int type;

    @Override
    public void initSucessView() {
        trans = (CourseInterTrans) getTransModels();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        if(null == trans)return;
        type = trans.type;
        boolean isCourseInter = (type == ApplicationConfig.COURSE_INTER);
        back.setText((isCourseInter)?R.string.my_quiz:R.string.reply);
        title_linear.setVisibility(isCourseInter?View.VISIBLE:View.GONE);
        contentHint = (isCourseInter)?R.string.please_problem:R.string.please_edit_reply_content;
        content.setHint(contentHint);
    }

    @Event(value = {R.id.back,R.id.submit})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
            case R.id.submit:

                String titleStr = getEditToString(title);
                String contentStr = getEditToString(content);

                if((type == ApplicationConfig.COURSE_INTER) && !isRequestStr(titleStr)){
                    PromptManager.showToast(R.string.please_title_hint);
                    return;
                }

                if(!isRequestStr(contentStr)){
                    if(type == ApplicationConfig.COURSE_INTER){
                        PromptManager.showToast(R.string.please_problem_course);
                    }else if(type == ApplicationConfig.COURSE_INTER_ONE){
                        //一级分类
                        PromptManager.showToast(R.string.please_edit_reply_content);
                    }else if(type == ApplicationConfig.COURSE_INTER_TWO){
                        //二级分类
                        PromptManager.showToast(R.string.please_edit_reply_content);
                    }
                    return;
                }

                submit.setEnabled(false);

                //进行网络请求
                setReleaseInterlocu();
                break;
        }
    }

    private void setReleaseInterlocu(){

        /**
         “id”:1    //课程ID
         “title”:”标题” //提问标题
         “content”:”我遇到了这个问题” //问题描述

         */
        String contentStr = getEditToString(content);

        mapKeys.put(AppHttpKey.CONTENT,contentStr);

        if(type == ApplicationConfig.COURSE_INTER){
            mapKeys.put(AppHttpKey.ID,String.valueOf(trans.id));
            mapKeys.put(AppHttpKey.TITLE,getEditToString(title));

            AppHttpRequest.appQuitState(ApplicationConfig.ADD_COURSE_QUES,
                    mapKeys, new AppHttpRequest.OnAppHttpListener() {
                        @Override
                        public void onAppHttpState(boolean isState, String result) {
                            if(isState){
                                PromptManager.showToast(R.string.release_course_problem_sucess);
                                CourseReleaseInterlocuActivity.this.finish();

                                sendBroadcast(result);
                            }
                        }
                    });
        }else if(type == ApplicationConfig.COURSE_INTER_ONE){
            //一级回复
            mapKeys.put(AppHttpKey.ID,String.valueOf(trans.id));
            AppHttpRequest.appQuitState(ApplicationConfig.ADD_COURSE_ANSWER,
                    mapKeys, new AppHttpRequest.OnAppHttpListener() {
                        @Override
                        public void onAppHttpState(boolean isState, String result) {
                            if(isState){
                                PromptManager.showToast(R.string.release_reply_sucess);
                                CourseReleaseInterlocuActivity.this.finish();

                                sendBroadcast(result);
                            }
                        }
                    });
        }else if(type == ApplicationConfig.COURSE_INTER_TWO){
            //二级回复
            mapKeys.put(AppHttpKey.ID,String.valueOf(trans.id));
            mapKeys.put(AppHttpKey.PARENT_ID,String.valueOf(trans.parent_id));
            mapKeys.put(AppHttpKey.REPLY_USER_ID,String.valueOf(trans.reply_user_id));
            AppHttpRequest.appQuitState(ApplicationConfig.ADD_COURSE_ANSWER,
                    mapKeys, new AppHttpRequest.OnAppHttpListener() {
                        @Override
                        public void onAppHttpState(boolean isState, String result) {
                            if(isState){
                                PromptManager.showToast(R.string.release_reply_sucess);
                                CourseReleaseInterlocuActivity.this.finish();
                            }
                        }
                    });
        }
    }


    /**
     * 发送成功通知
     * @param result
     */
    private void sendBroadcast(String result){
        Intent intent = new Intent(ApplicationConfig.APP_QX_COURSE_INTER);
        intent.putExtra(TransformController.ChangeKEY.INTENT_KEY, result);
        LocalBroadcastManager.getInstance(QXApplication.getContext()).sendBroadcast(intent);
    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        submit.setEnabled(false);
        submit.setBackgroundResource(R.drawable.ash_btn_frame);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(isEmptyOrHintMinCount(content,contentHint,1)){
            if(type == ApplicationConfig.COURSE_INTER){
                submit.setEnabled(isEmptyOrHint(title,R.string.please_title_hint));
                submit.setBackgroundResource(0);
                submit.setBackgroundResource(isEmptyOrHint(title,R.string.please_title_hint)?
                                                R.drawable.blue_btn_frame:
                                                R.drawable.ash_btn_frame);
                return;
            }
            submit.setEnabled(true);
            submit.setBackgroundResource(R.drawable.blue_btn_frame);
        }else{
            submit.setEnabled(false);
            submit.setBackgroundResource(0);
            submit.setBackgroundResource(R.drawable.ash_btn_frame);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {}


    @Override
    public void initViewHandler() {
        title.addTextChangedListener(this);
        content.addTextChangedListener(this);
    }

}